<template>
  <view class="theme-toggle">
    <button @click="handleToggle" class="theme-button" :class="buttonClass">
      <text v-if="!showAsIcon" class="button-text">{{ buttonText }}</text>
      <text class="theme-icon" :class="{ 'icon-only': showAsIcon }">{{ iconText }}</text>
    </button>
    
    <!-- 主题选择弹窗 -->
    <view v-if="showModal" class="modal-overlay" @click="closeModal">
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <text class="modal-title">选择主题</text>
          <button class="close-btn" @click="closeModal">×</button>
        </view>
        <view class="theme-options">
          <view 
            class="theme-option" 
            :class="{ active: currentTheme === 'theme1' }"
            @click="selectTheme('theme1')"
          >
            <view class="theme-preview theme1-preview"></view>
            <text class="theme-name">主题色1</text>
            <text class="theme-desc">清新蓝色</text>
          </view>
          <view 
            class="theme-option" 
            :class="{ active: currentTheme === 'theme2' }"
            @click="selectTheme('theme2')"
          >
            <view class="theme-preview theme2-preview"></view>
            <text class="theme-name">主题色2</text>
            <text class="theme-desc">温暖棕色</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'ThemeToggle',
  props: {
    currentTheme: {
      type: String,
      default: 'theme1',
      validator: (value) => ['theme1', 'theme2'].includes(value)
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    position: {
      type: String,
      default: 'right',
      validator: (value) => ['left', 'center', 'right'].includes(value)
    },
    showAsIcon: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showModal: false
    };
  },
  computed: {
    buttonText() {
      return this.currentTheme === 'theme1' ? '切换到主题2' : '切换到主题1';
    },
    iconText() {
      return this.showAsIcon ? '⚙️' : '🎨';
    },
    buttonClass() {
      return [
        `theme-button--${this.size}`,
        `theme-button--${this.currentTheme}`,
        { 'theme-button--icon-only': this.showAsIcon }
      ];
    }
  },
  methods: {
    handleToggle() {
      this.showModal = true;
    },
    closeModal() {
      this.showModal = false;
    },
    selectTheme(theme) {
      // 保存到本地存储
      uni.setStorageSync('currentTheme', theme);
      // 触发父组件事件
      this.$emit('theme-change', theme);
      // 广播全局主题变化事件
      uni.$emit('global-theme-change', theme);
      // 关闭弹窗
      this.closeModal();
    }
  }
};
</script>

<style scoped>
.theme-toggle {
  display: flex;
  justify-content: v-bind(position === 'left' ? 'flex-start' : position === 'center' ? 'center' : 'flex-end');
  margin-bottom: 15px;
}

.theme-button {
  display: flex;
  align-items: center;
  gap: 8px;
  border: none;
  background-color: transparent;
  color: #666;
  border-radius: 20px;
  cursor: pointer;
}

.theme-button:hover {
  color: #007aff;
}

.theme-button:active {
  opacity: 0.8;
}

/* 尺寸变体 */
.theme-button--small {
  padding: 6px 12px;
  font-size: 12px;
}

.theme-button--medium {
  padding: 8px 16px;
  font-size: 14px;
}

.theme-button--large {
  padding: 10px 20px;
  font-size: 16px;
}

/* 主题变体 */
.theme-button--theme1 {
  color: #007aff;
}

.theme-button--theme2 {
  color: #8b7355;
}

.theme-button--theme1:hover,
.theme-button--theme2:hover {
  opacity: 0.7;
}

/* 图标模式样式 */
.theme-button--icon-only {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  padding: 0;
  justify-content: center;
  min-width: auto;
}

.theme-button--icon-only.theme-button--small {
  width: 32px;
  height: 32px;
  font-size: 14px;
}

.theme-button--icon-only.theme-button--medium {
  width: 40px;
  height: 40px;
  font-size: 16px;
}

.theme-button--icon-only.theme-button--large {
  width: 48px;
  height: 48px;
  font-size: 18px;
}

.theme-icon.icon-only {
  margin: 0;
}

.button-text {
  white-space: nowrap;
}

.theme-icon {
  font-size: 16px;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.modal-content {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin: 20px;
  max-width: 400px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #666;
}

.theme-options {
  display: flex;
  gap: 16px;
  flex-direction: column;
}

.theme-option {
  display: flex;
  align-items: center;
  padding: 16px;
  border: 2px solid #f0f0f0;
  border-radius: 8px;
  cursor: pointer;
  background-color: #fafafa;
}

.theme-option:hover {
  border-color: #d0d0d0;
  background-color: #f5f5f5;
}

.theme-option.active {
  border-color: #007aff;
  background-color: #f0f8ff;
}

.theme-preview {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 16px;
  flex-shrink: 0;
}

.theme1-preview {
  background: linear-gradient(135deg, #4A90E2, #E8F4FD);
  border: 2px solid #D1E9F6;
}

.theme2-preview {
  background: linear-gradient(135deg, #A0522D, #F5E6D3);
  border: 2px solid #E8D5C4;
}

.theme-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-right: 8px;
}

.theme-desc {
  font-size: 14px;
  color: #666;
}
</style>